﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShevaEngine.Core.Modules.Materials
{
    /// <summary>
    /// Graphics material parameters list.
    /// </summary>
    public class GraphicsMaterialParametersList : List<GraphicsMaterialParameter>
    {
        /// <summary>Is animated.</summary>
        public Boolean IsAnimated { get { return this.AnimatedParameters != null; } }
        /// <summary>Animated parameters.</summary>
        public GraphicsMaterialParameterAnimations AnimatedParameters { get; private set; }

        /// <summary>
        /// Constructor.
        /// </summary>
        public GraphicsMaterialParametersList()
            : base()
        {

        }

        /// <summary>
        /// Method adds parameter.
        /// </summary>
        /// <param name="parameter"></param>
        public new void Add(GraphicsMaterialParameter parameter)
        {
            if (parameter is GraphicsMaterialParameterAnimations)
                this.AnimatedParameters = parameter as GraphicsMaterialParameterAnimations;

            base.Add(parameter);
        }

        /// <summary>
        /// Method setup all parameters.
        /// </summary>
        /// <param name="material"></param>
        public void SetParameters(GraphicsMaterial material)
        {
            for (int i = this.Count - 1; i >= 0; i--)
                this[i].Set(material);
        }
    }

    /// <summary>
    /// Graphics material parameters list equality comparer.
    /// </summary>
    public class GraphicsMaterialParametersListEqualityComparer : IEqualityComparer<GraphicsMaterialParametersList>
    {
        /// <summary>
        /// Methods compares two items.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool Equals(GraphicsMaterialParametersList x, GraphicsMaterialParametersList y)
        {
            if (x.IsAnimated || y.IsAnimated)
                return false;

            foreach (GraphicsMaterialParameter yParameter in y)
            {
                bool fit = false;

                foreach (GraphicsMaterialParameter xParameter in x)
                    if (yParameter.GetType() == xParameter.GetType())
                        if (yParameter.IsSame(xParameter))
                            fit = true;

                if (!fit)
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Get hash code.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int GetHashCode(GraphicsMaterialParametersList obj)
        {
            return base.GetHashCode();
        }
    }
}
